import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/8 16:04
 * @description
 */
public class Solution2 {
    public static void main(String[] args) {

    }
    public int findPairs(int[] nums, int k) {
        int result = 0;
        Arrays.sort(nums);
        int left = 0;
        int right = 0;
        while (right < nums.length) {
            while (left < right && nums[right] - nums[left] > k) {
                left++;
            }
            if (left < right && nums[right] - nums[left] == k) {
                result++;
                while (left < right && nums[left] == nums[left + 1]) {
                    left++;
                }
                while (right < nums.length - 1 && nums[right] == nums[right + 1]) {
                    right++;
                }
            }
            right++;
        }
        return result;
    }
}
